package com.dlyk.web;

import com.dlyk.constant.Constants;
import com.dlyk.model.Activity;
import com.dlyk.query.ActivityQuery;
import com.dlyk.result.R;
import com.dlyk.service.ActivityService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 市场活动
 */
@RestController
public class ActivityController {

    @Resource
    private ActivityService activityService;

    /**
     * 查询市场活动（PageHelper 分页查询）
     */
    // 要想使用@PreAuthorize注解，需要在Spring Security配置类中使用@EnableMethodSecurity注解，开启方法级别的权限检查
    // 当前用户有 "activity:list" 这个权限标识符时才可以访问这个控制器
    @PreAuthorize(value = "hasAuthority('activity:list')")
    @GetMapping("/api/activity/list")
    public R activityListPage(@RequestParam(value = "current", required = false) Integer current,
                              ActivityQuery activityQuery) {

        // required = false  表示非必须，这个参数也可以不传，不传参数，默认查询第一页
        if (current == null) {
            current = 1;
        }

        // ActivityQuery对象用于接收条件查询的参数

        PageInfo<Activity> activityPageInfo = activityService.getActivityListByPage(current, activityQuery);

        return R.OK(activityPageInfo);
    }

    /**
     * 录入市场活动
     */
    @PreAuthorize(value = "hasAuthority('activity:add')")
    @PostMapping("/api/activity/add")
    public R addActivity(ActivityQuery activityQuery, Authentication authentication) {

        int save = activityService.saveActivity(activityQuery, authentication);

        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 根据id获取市场活动信息
     */
    @GetMapping("/api/activity/get/{id}")
    public R getActivity(@PathVariable("id") Integer id) {

        Activity activity = activityService.getActivityById(id);

        return R.OK(activity);
    }

    /**
     * 编辑市场活动
     *
     * 还可以使用token获取当前登录用户，使用@RequestHeader注解获取请求头中的token
     */
    @PreAuthorize(value = "hasAuthority('activity:edit')")
    @PutMapping("/api/activity/edit")
    public R editActivity(ActivityQuery activityQuery, @RequestHeader(value = Constants.TOKEN_NAME) String token) {

        int edit = activityService.editActivity(activityQuery, token);

        return edit >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 根据id查询市场活动详情
     */
    @PreAuthorize(value = "hasAuthority('activity:detail')")
    @GetMapping("/api/activity/detail/{id}")
    public R activityDetail(@PathVariable("id") Integer id) {

        Activity activity = activityService.getActivityDetailById(id);

        return R.OK(activity);
    }

    /**
     * 删除市场活动
     */
    @PreAuthorize(value = "hasAuthority('activity:remove')")
    @DeleteMapping("/api/activity/remove/{id}")
    public R removeActivity(@PathVariable("id") Integer id) {

        int remove = activityService.removeActivityById(id);

        return remove >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 批量删除市场活动
     */
    @PreAuthorize(value = "hasAuthority('activity:remove')")
    @DeleteMapping("/api/activity/batchRemove")
    // 这里前端传递的参数是String类型，后端SpringMVC会自动进行类型转换（需要使用@RequestParam注解）
    public R batchRemoveActivity(@RequestParam("ids") List<String> ids) {

        int batchRemove = activityService.batchRemoveActivity(ids);

        return batchRemove >= ids.size() ? R.OK() : R.FAIL();
    }

}

